import { Component } from '@angular/core';
import { NavController, NavParams } from '@ionic/angular';
import { ShopService } from '../services/shop.service';
import * as constants from '../providers/Constants'
import { ToastService } from './../providers/toast/toast.service'
import { Router } from '@angular/router';
import { Helper } from '../providers/Helper';
import { Storage } from '../providers/Storage';
import { Utils } from '../providers/Utils';
@Component({
  selector: 'app-tab2',
  templateUrl: 'tab2.page.html',
  styleUrls: ['tab2.page.scss']
})
export class Tab2Page {
  // categoryData: any = this.getCategoryData();
  categoryData: any = [];
  public categoryDetailData = [];
  public select = 0;
  searchValue: string = 'Search Product';
  imageUrl: string = '';
  startPageIndex: number = 0;
  queryStr: string = '';
  data: any = {
    nowPage: 1,
    pageSize: 100,
    procClassId: '',
    procName: this.queryStr//产品名称
  }

  classifyData:any=[];
  constructor(public nav: NavController, private mHelper: Helper, public router: Router, public service: ShopService, private toast: ToastService) {
    this.imageUrl = this.service.getImageUrl();
    this.queryProcClass('');
  }
  // 界面重新进入时，轮播图自动播放
  ionViewWillEnter() {
    // let value= Storage.localStorage.get('searchValue');
    // if (value!=null) {
    //   this.searchValue=value;
    // }

  }

  getItems(e) {
    console.log(e.detail.value);
    this.data.procName = e.detail.value;
    // this.getList(this.startPageIndex);
  }

  startPage(index: number, data: any) {
    console.log('0000' + index, data);
    this.startPageIndex = index;
    this.nav.navigateForward(['/goods-details'], {
      queryParams: {
        shopInfo: JSON.stringify(data)
      }
    });

  }
  solaImage(img){
    console.log("看看这里：",this.imageUrl)
    console.log("看看这里1：",img)
    return Utils.imageUrl(this.imageUrl,img);
  }

  queryProcClass(procParentClassId) {
    let m = 'ios'
    if (this.mHelper.isAndroid()) {
      m = 'android'
    }
    this.service.queryProcClass({
      // mode: m,
      procParentClassId:procParentClassId,
      type:'1',
    }).subscribe(res => {
      console.log(JSON.stringify(res));
      if (res.retCode == '000000') {
        res.data.forEach((element, index) => {
          if (index == 0) {
            // this.data.procClassId = element.procClassId;
            this.queryProcClass1(element.procClassId);
            element.isSelect = true;
          } else {
            element.isSelect = false;
          }
        });
        this.categoryData = res.data;
      } else {
        this.toast.show("exception service routine");
      }
    })
  }
  queryProcClass1(procParentClassId,index:number=0) {
    this.classifyData=[];
    this.service.queryProcClass({
      // mode: m,
      // procClassId:procParentClassId,
      procParentClassId:procParentClassId,
      type:'1',
    }).subscribe(res => {
      console.log(JSON.stringify(res));
      this.categoryData[this.select].isSelect = false;
        let data = this.categoryData[index];
        data.isSelect = true;
        this.select = index;
      if (res.retCode == '000000') {
        // res.data.forEach((element, index) => {
       
        // });
        this.classifyData = res.data;
      } else {
        this.toast.show("exception service routine");
      }
    })
  }
  AmountOfConversion(num: number) {
    return num / 100;
  }
  onItem(item){
    this.nav.navigateForward('goods-search-list',{
      queryParams:{
        searchType:0,
        goodsInfo:JSON.stringify(item)
      }
    })
  }
  
  search(){
    console.log('---------------------');
    this.nav.navigateForward(['/goods-search'], {
      queryParams: {
        type: '2' //分类
      }
    })
  }

  categoryLeftClick(index: number, procClassId) {
    console.log("index" + index, 'procClassId=' + procClassId);
    this.data.procClassId = procClassId;
    this.queryProcClass1(procClassId,index);
  };






  private getCategoryData() {
    return [
      {
        procClassName: "潮流女装",
        typeNumber: '102'
        , isSelect: true
      },
      {
        procClassName: "品牌男装",
        typeNumber: '103'
        , isSelect: false
      },
      {
        procClassName: "热门推荐",
        typeNumber: '101'
        , isSelect: false
      },
      {
        procClassName: "内衣配饰",
        typeNumber: '104'
        , isSelect: false
      },
      {
        procClassName: "家用电器",
        typeNumber: '105'
        , isSelect: false
      },
      {
        procClassName: "电脑办公",
        typeNumber: '106'
        , isSelect: false
      },
      {
        procClassName: "手机数码",
        typeNumber: '107'
        , isSelect: false
      },
      {
        procClassName: "母婴频道",
        typeNumber: '108'
        , isSelect: false
      },
      {
        procClassName: "图书",
        typeNumber: '109'
        , isSelect: false
      },
      {
        procClassName: "家居家纺",
        typeNumber: '110'
        , isSelect: false
      },
      {
        procClassName: "居家生活",
        typeNumber: '111'
        , isSelect: false
      },
      {
        procClassName: "家具建材",
        typeNumber: '112'
        , isSelect: false
      },
      {
        procClassName: "热门推荐",
        typeNumber: '101', isSelect: false
      },
      {
        procClassName: "居家生活",
        typeNumber: '101', isSelect: false
      }
    ];

  }
}
